package com.xw.wuliu;



import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import com.xw.model.FuncData;
import com.xw.wuliu.adapter.FuncAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class FuncEnterActivity extends Activity {
	private String userPaName = "";
//	private static final String PREFS_SIGNFLAG = "signFlag";
//	private int signFlag = 0; // 0为下班 1为上班
	private TextView tvWelcome = null;
	
	// 九宫格
	private GridView gridView = null;
	private FuncAdapter mAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.funcmain);
		setupView();
//		startLocBackSvc();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
//		stopLocBackService();
		super.onDestroy();
	}

	public String GetTime()
	{
		 Calendar c =  Calendar.getInstance(Locale.UK);
		 c.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
		 int year  = c.get(Calendar.YEAR);
		 int month = c.get(Calendar.MONTH);
		 int day = c.get(Calendar.DAY_OF_MONTH);
		 int hour = c.get(Calendar.HOUR_OF_DAY);
		 int min = c.get(Calendar.MINUTE);
		 int sec = c.get(Calendar.SECOND);
		 
		 String timeStr = year+"."+(month+1)+"."+day+" "+hour+":"+min;
		 return timeStr;
		
		
	}
	
	/**
	 * @function 开启后台位置上传服务
	 * 
	 */
	public void startLocBackSvc()
	{
		
		Intent i = new Intent();
		i.setAction("com.xw.locationsvc");
		this.startService(i);
		
	}
	
	/**
	 *  @function 停止后台位置上传服务
	 * 
	 * 
	 */
	public void stopLocBackService()
	{
		Intent i = new Intent();
		i.setAction("com.xw.locationsvc");
		this.stopService(i);
	}
	
	public void setupView() {
		
		userPaName = this.getIntent().getExtras().getString("username");
//		loadSignFlag();
		tvWelcome = (TextView) findViewById(R.id.tvWelcome);
		
		String welcome = this.getResources().getString(R.string.welcome);
		String strTime = GetTime();
		tvWelcome.setText(welcome+" 现在是"+strTime);
		
		gridView = (GridView) findViewById(R.id.myGrid);
		
//		if (signFlag == 0) {
//			name[1] = "上班签到";
//			signFlag = 1;
//		}
//		else{
//			name[1] = "下班签到";
//			signFlag = 0;
//		}
//		System.out.println("signFlag:"+signFlag);
		
		mAdapter = new FuncAdapter(this,FuncData.getDefaultData());
		gridView.setAdapter(mAdapter);
		
		gridView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				
				Intent i = new Intent();
				i.putExtra("userPaName", userPaName);
				
				switch(position)
				{
				case 0:
					i.setClass(FuncEnterActivity.this, ServiceListActivity.class);
					startActivity(i);
					
					break;
				case 1:
//					saveSignFlag();
//					改变数组的值
//					放到adapter里面
//					xinjian yige 新建一个adapter
//					ranhou chongxin shezhi yichongxinchongxinff重新设置到fridview上
					i.setClass(FuncEnterActivity.this, SignActivity.class);
					startActivity(i);
					//register();
					
					break;
				case 2:
					break;
				case 3:
					break;
				case 4:
					i.setClass(FuncEnterActivity.this, MapSignInActivity.class);
					startActivity(i);
					
					break;
				case 5:
					break;
				default:
					break;
				}
				
				
				
			}
			
		});
	}
	
//	//签到
//	private void register(){
//		//得到用户名
//		String username = this.getIntent().getExtras().getString("username");
//		System.out.println("username:"+username);
//		
//		//获取经纬度
//		double latitude = 0.0;
//		double longitude = 0.0;
//		LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//		if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
//			Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//			if(location != null){
//				latitude = location.getLatitude();
//				longitude = location.getLongitude();
//			}else{
//					
//			}
//		}else{
//			LocationListener locationListener = new LocationListener() {
//				
//				// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
//				@Override
//				public void onStatusChanged(String provider, int status, Bundle extras) {
//					
//				}
//				
//				// Provider被enable时触发此函数，比如GPS被打开
//				@Override
//				public void onProviderEnabled(String provider) {
//					
//				}
//				
//				// Provider被disable时触发此函数，比如GPS被关闭 
//				@Override
//				public void onProviderDisabled(String provider) {
//					
//				}
//				
//				//当坐标改变时触发此函数，如果Provider传进相同的坐标，它就不会被触发 
//				@Override
//				public void onLocationChanged(Location location) {
//					if (location != null) {   
//						Log.e("Map", "Location changed : Lat: "  
//						+ location.getLatitude() + " Lng: "  
//						+ location.getLongitude());   
//					}
//				}
//			};
//			locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000, 0,locationListener);   
//			Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);   
//			if(location != null){   
//				latitude = location.getLatitude(); //经度   
//				longitude = location.getLongitude(); //纬度
//			}   
//		}
//
//		System.out.println("latitude:"+latitude + "longitude:"+longitude);
//		
////		LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
////		Criteria criteria = new Criteria(); 
////        criteria.setAccuracy(Criteria.ACCURACY_FINE);//高精度 
////        criteria.setAltitudeRequired(false);//不要求海拔 
////        criteria.setBearingRequired(false);//不要求方位 
////        criteria.setCostAllowed(true);//允许有花费 
////        criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗 
////
////       //从可用的位置提供器中，匹配以上标准的最佳提供器 
////        String provider = locationManager.getBestProvider(criteria, true); 
////        
////        //获得最后一次变化的位置 
////        Location location = locationManager.getLastKnownLocation(provider); 
////		System.out.println("latitude:"+location.getLatitude() + "longitude:"+location.getLongitude());
//		
//		// 获取当前时间
//		SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd/HH:mm:ss");
//		Date curDate = new Date(System.currentTimeMillis());
//		String timeStr = formatter.format(curDate);
//		System.out.println("timeStr:"+timeStr);
//
//	}
	
	private List<Map<String, Object>> configMenu(String[] name, int[] img) {
		if (name.length != img.length) {
			return null;
		}
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < name.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ItemImage", img[i]);
			map.put("ItemText", name[i]);
			list.add(map);
		}
		return list;
	}

//	private void saveSignFlag(){
//		//载入配置文件
//		SharedPreferences sp = getSharedPreferences(PREFS_SIGNFLAG, 0);
//		//写入配置文件
//		Editor spEd = sp.edit();
//		spEd.putInt("signFlag", signFlag);
//		spEd.commit();
//	}
//	
//	private void loadSignFlag(){
//		SharedPreferences sp = getSharedPreferences(PREFS_SIGNFLAG, 0);
//		signFlag = sp.getInt("signFlag", 0);
//	}
	
	/*
	public final class ImageAdapter extends BaseAdapter {
		private Context m_context;

		public ImageAdapter(Context context) {
			this.m_context = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mThumbIds.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			ImageView imgeview;
			if(convertView == null)
			{
				imgeview = new ImageView(m_context);
				imgeview.setLayoutParams(new GridView.LayoutParams(45, 45));
				imgeview.setPadding(8, 8, 8, 8);
				imgeview.setAdjustViewBounds(false);
				imgeview.setScaleType(ImageView.ScaleType.CENTER_CROP);
			}
			else
			{
				imgeview = (ImageView) convertView;
			}
			imgeview.setImageResource(mThumbIds[position]);
			
			return imgeview;
		}

		private Integer[] mThumbIds = { R.drawable.ic_launcher,
				R.drawable.ic_launcher, R.drawable.ic_launcher,
				R.drawable.ic_launcher, R.drawable.ic_launcher,
				R.drawable.ic_launcher, R.drawable.ic_launcher

		};

	}*/

}
